package fr.pud.client.view.mapstation;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Transparency;

import javax.swing.JButton;

import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;

import fr.pud.client.controller.bubble.ShowBubble;
import fr.pud.client.model.Address;
import fr.pud.client.model.PUDStation;
import fr.pud.client.model.Vehicle;
import fr.pud.client.view.JMapViewerExtended;
import fr.pud.client.view.MapQueries;

/**
 * This class allows the user to localize a station on the map using a button
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 */
public class MapStation implements MapMarker {
    /**
     * An int to define the blue pointer.
     */
    public static final int BLUE_POINTER   = 0;
    /**
     * An int to define the green pointer.
     */
    public static final int GREEN_POINTER  = 1;
    /**
     * An int to define the orange pointer.
     */
    public static final int ORANGE_POINTER = 2;
    /**
     * An int to define the red pointer.
     */
    public static final int RED_POINTER    = 3;
    /**
     * An int to define the purple pointer.
     */
    public static final int PURPLE_POINTER = 4;
    /**
     * refers to the station of the MapStation
     */
    private PUDStation      station;
    /**
     * The address will be visit by this vehicle
     */
    private Vehicle         vehicle;
    /**
	 * 
	 */
    private String          numberPointers;
    /**
     * The label of the pointer
     */
    private JButton         butonPointer;
    /**
     * A boolean which precise that the MapStation was added by the context menu
     */
    private boolean         addByContextMenu;
    /**
     * A boolean which precise if the mapStation has to painted or not
     */
    private boolean         isVisible      = true;

    /**
     * A constructor which creates a MapStation with a station and the number of
     * pointers
     * @param station
     *            , the station
     * @param s
     *            , the number of pointers
     */
    public MapStation(PUDStation station, String s, Vehicle vehicle,
            int colorMarker) {
        this.station = station;
        this.numberPointers = s;
        this.butonPointer = this.createPointer(colorMarker);
        JMapViewerExtended.getJMapViewer().add(this.butonPointer);
        this.vehicle = vehicle;
        this.addByContextMenu = false;
    }

    /**
     * A constructor which creates a MapStation with a station
     * @param a
     *            where the MapStation will be drawn
     */
    public MapStation(PUDStation s, Vehicle vehicle, int colorMarker) {
        this.station = s;
        this.numberPointers = " ";
        this.butonPointer = this.createPointer(colorMarker);
        JMapViewerExtended.getJMapViewer().add(this.butonPointer);
        this.vehicle = vehicle;
        this.addByContextMenu = false;
    }

    /**
     * /**
     * @return a new pointer of an address
     */
    public JButton createPointer(int colorMarker) {
        ButtonPointer pointer = new ButtonPointer(this.numberPointers,
                colorMarker);
        pointer.setBorderPainted(false);
        pointer.setOpaque(false);
        pointer.setBackground(new Color(Transparency.OPAQUE));
        pointer.addMouseListener(new ShowBubble(pointer, this.station));
        return pointer;
    }

    /**
     * @return the address of the mapStation
     */
    public Address getAddress() {
        return this.station.getAddress();
    }

    /**
     * @return the coordinates of the address
     */
    public Coordinate getCoordonneesAddress() {
        return this.station.getAddress().getCoordinate();
    }

    /**
     * @return the latitude of the address
     */
    @Override
    public double getLat() {
        return this.station.getAddress().getCoordinate().getLat();
    }

    /**
     * @return the longitude of the address
     */
    @Override
    public double getLon() {
        return this.station.getAddress().getCoordinate().getLon();
    }

    /**
     * @return the number of pointers
     */
    public String getNumberPointers() {
        return this.numberPointers;
    }

    /**
     * @return the button used to signalize the address
     */
    public JButton getPointer() {
        return this.butonPointer;
    }

    /**
     * @return the station of the mapStation
     */
    public PUDStation getStation() {
        return this.station;
    }

    public Vehicle getVehicle() {
        return this.vehicle;
    }

    public boolean isAddedByContextMenu() {
        return this.addByContextMenu;
    }

    /**
     * @return the isVisible
     */
    public boolean isVisible() {
        return this.isVisible;
    }

    /**
     * Paint the pointer of the address on the graphic
     */
    @Override
    public void paint(Graphics g, Point position) {
        if (JMapViewerExtended.getJMapViewer().getMapMarkersVisible()
                && position != null
                && JMapViewerExtended.getJMapViewer().getZoom() > 6) {
            int x1 = position.x - this.getPointer().getHeight() + 18;
            int y1 = position.y - this.getPointer().getWidth() / 2 - 18;
            this.getPointer().setVisible(true);
            this.getPointer().setBounds(x1, y1, 20, 30);
        }
        else {
            this.getPointer().setVisible(false);
        }
    }

    public void paintQueriesMarkers(Graphics g, Point position) {
        if (MapQueries.getInstance().isMapQueriesVisible()
                && JMapViewerExtended.getJMapViewer().getMapMarkersVisible()) {
            this.paint(g, position);
        }
        else {
            this.getPointer().setVisible(false);
        }
    }

    public void paintRoutesMarkers(Graphics g, Point position,
            boolean mapRouteIsVisible) {
        if (mapRouteIsVisible
                && JMapViewerExtended.getJMapViewer().getMapMarkersVisible()) {
            this.paint(g, position);
        }
        else {
            this.getPointer().setVisible(false);
        }
    }

    public void paintSugestionsMarkers(Graphics g, Point position) {
        if (JMapViewerExtended.getJMapViewer().isMapSuggestionVisible()
                && JMapViewerExtended.getJMapViewer().getMapMarkersVisible()) {
            this.paint(g, position);
        }
        else {
            this.getPointer().setVisible(false);
        }
    }

    /**
     * @param isVisible
     *            the isVisible to set
     */
    public void setVisible(boolean isVisible) {
        this.getPointer().setVisible(isVisible);
        this.isVisible = isVisible;
    }
}
